package com.vf.admin.common;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;


import javax.sql.DataSource;


/**
 * 配置多少多数据源(用于分库)
 */
@Configuration
public class DataSourceConfig {
    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource primaryDataSource() {
        //硬编码方式设置数据库连接参数
//        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
//        dataSource.setDbType("com.alibaba.druid.pool.DruidDataSource");
//        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
///*        dataSource.setUrl("jdbc:mysql://clb-db.ccmm9rv0djes.ap-northeast-1.rds.amazonaws.com:3306/qukuailian?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
//        dataSource.setUsername("qukuailian_dbuser");
//        dataSource.setPassword("oE^xIHu%fL^xGkh5pDgY");*/
//
//        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/qukuailian?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
//        dataSource.setUsername("root");
//        dataSource.setPassword("root");
//
//        dataSource.setTimeBetweenEvictionRunsMillis(60000);
//        dataSource.setMinEvictableIdleTimeMillis(300000);
//        dataSource.setInitialSize(30);
//        dataSource.setMaxActive(50);
//        dataSource.setMinIdle(30);
//        dataSource.setMaxWait(60000);
//        dataSource.setTimeBetweenEvictionRunsMillis(60000);
//        dataSource.setMinEvictableIdleTimeMillis(30000);
//        dataSource.setValidationQuery("SELECT 1 FROM DUAL");
//        dataSource.setTestWhileIdle(true);
//        dataSource.setTestOnBorrow(true);
//        dataSource.setTestOnReturn(false);
//        dataSource.setPoolPreparedStatements(true);
//        dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
//        dataSource.setValidationQueryTimeout(30000);
//        return dataSource;

        //默认读取yml文件
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 主数据源事务
     * @param dataSource dataSource
     * @return PlatformTransactionManager
     */
    @Primary
    @Bean("platformTransactionManager")
    public PlatformTransactionManager platformTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }


//    @Bean(name = "secondaryDataSource")
//    @Qualifier("secondaryDataSource")
//    @Primary
//    @ConfigurationProperties(prefix="spring.datasource.secondary")
//    public DataSource secondaryDataSource() {
//    DruidDataSourceBuilder
//        return DataSourceBuilder.create().build();
//    }
//    @Bean(name = "secondaryJdbcTemplate")
//    public JdbcTemplate secondaryJdbcTemplate(
//            @Qualifier("secondaryDataSource") DataSource dataSource) {
//        return new JdbcTemplate(dataSource);
//    }

//    /**
//     * 从数据源事务
//     * @param dataSource dataSourceTwo
//     * @return PlatformTransactionManager
//     */
//    @Bean("platformTransactionManagerTwo")
//    public PlatformTransactionManager platformTransactionManagerTwo(@Qualifier("secondaryDataSource") DataSource dataSource) {
//        return new DataSourceTransactionManager(dataSource);
//    }

}